package vn.com.pavibook.admin.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.com.pavibook.admin.service.ManageTransactionService;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.TransactionVO;
import vn.com.pavibook.utils.DateUtils;

@Controller
public class ManageTransactionController {

	private static final int offset = 10;

	private static final Logger logger = LoggerFactory
			.getLogger(ManageBookController.class);

	@Autowired
	private ManageTransactionService manageTransactionService;

	// info statistic to day
	@RequestMapping(value = "/admin/transaction-info", method = RequestMethod.GET)
	public String openManageTransaction(Model model,
			@RequestParam("typeStatistic") String typeStatistic) {
		logger.debug("manage transection today");

		// get role
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		Object user = context.getAuthentication().getPrincipal();
		String role = StringUtils.EMPTY;
		if (user instanceof User) {
			User userDetail = (User) user;
			for (GrantedAuthority ga : userDetail.getAuthorities()) {
				// TODO save to list with account has many role access
				role = ga.getAuthority();
			}
		}

		PagingInfo pagingInfo = new PagingInfo(offset, 1);
		// String typeStatistic =
		// DataHelper.TypeStatistic.TYPE_STATISTIC_TODAY.getCode();
		SearchResult<TransactionVO> result = manageTransactionService
				.getTransection(pagingInfo, null, typeStatistic, username,
						role, null);
		List<TransactionVO> listTransaction = result.getSearchResult();
		PagingInfo pagingInfoNew = result.getPagingInfo();
		model.addAttribute("listTransaction", listTransaction);
		model.addAttribute("pagingInfo", pagingInfoNew);
		model.addAttribute("typeStatistic", typeStatistic);
		return "admin/transaction-info";
	}

	// search all
	@RequestMapping(value = "/admin/searchTransaction", method = RequestMethod.POST)
	public String searchTransaction(Model model,
			@RequestParam("pageIndex") int pageIndex,
			@RequestParam("keyword") String keyword,
			@RequestParam("typeSearch") String typeSearch,
			@RequestParam("typeStatistic") String typeStatistic) {

		// get role
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		Object user = context.getAuthentication().getPrincipal();
		String role = StringUtils.EMPTY;
		if (user instanceof User) {
			User userDetail = (User) user;
			for (GrantedAuthority ga : userDetail.getAuthorities()) {
				role = ga.getAuthority();
			}
		}

		PagingInfo pagingInfo = new PagingInfo(offset, pageIndex);
		SearchResult<TransactionVO> result = new SearchResult<TransactionVO>();
		// String typeStatistic =
		// DataHelper.TypeStatistic.TYPE_STATISTIC_ALL.getCode();
		if (StringUtils.isEmpty(keyword)) {
			result = manageTransactionService.getTransection(pagingInfo, null,
					typeStatistic, username, role, typeSearch);
		} else {
			result = manageTransactionService.getTransection(pagingInfo,
					keyword, typeStatistic, username, role, typeSearch);
		}
		List<TransactionVO> listTransaction = result.getSearchResult();

		model.addAttribute("listTransaction", listTransaction);
		model.addAttribute("pagingInfo", pagingInfo);
		model.addAttribute("keyword", keyword);
		model.addAttribute("typeSearch", typeSearch);
		model.addAttribute("typeStatistic", typeStatistic);
		return "admin/transaction-info";
	}

	@RequestMapping(value = "/admin/transaction-money", method = RequestMethod.GET)
	public String viewMoneyStatistic() {
		return "admin/transaction-money";
	}
	
	@RequestMapping(value = "/admin/viewMoneyStatistic", method = RequestMethod.POST)
	public @ResponseBody double getMoneyStatistic(@RequestParam("fromDate") String fromDateStr, 
			@RequestParam("toDate") String toDateStr) {
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		Object user = context.getAuthentication().getPrincipal();
		String role = StringUtils.EMPTY;
		if (user instanceof User) {
			User userDetail = (User) user;
			for (GrantedAuthority ga : userDetail.getAuthorities()) {
				role = ga.getAuthority();
			}
		}
		String fromDate = null;
		String toDate = null;
		
		if(StringUtils.isEmpty(fromDateStr)) {
			fromDate = DateUtils.formatDateToString(new Date());
		} else {
			fromDate = DateUtils.formatNormalDateString(fromDateStr);
			if(!StringUtils.isEmpty(toDateStr)) {
				toDate = DateUtils.formatNormalDateString(toDateStr);
			}
			
		}
		
		return manageTransactionService.getMoneyStatistic(username, role, fromDate, toDate);
	}
}
